;
(function(factory) {
	if(typeof define === 'function' && define.amd) {
		define(['jquery', 'vw.searchtree', 'vw.edittable', 'vw.bindreport', 'vw.map', 'vw.static', "bDialog"], factory);
	} else {
		if(typeof module === 'Object' && module.exports) {
			//
		} else {
			factory(window.jQuery);
		}
	}
})(function($) {
	'use strict';
	var pluginName = "vw_structure";
	var defaults = {
		hasContainerFluid: false,
		leftMargin: 100,
		contentMargin: 100,
		interfaceAddress: null,
		extrapara: {},
		onInitialized: undefined,
		onTabShown: undefined,
		activeTab: 0,
		/*searchtree*/
		showSearchtree: true,
		searchtree: null,
		treeDataURL: 'GetCatalog',
		treeInfoURL: 'GetTableInfo',
		treeSaveURL: 'SaveTableInfo',
		treeDeleteURL: 'DeleteTable',
		/*edittable*/
		showEdittable: true,
		edittable: null,
		locateTo: undefined,
		tableDataURL: 'LoadData',
		tableSaveURL: 'UpdateData',
		tableSubmitURL: 'InsertData',
		tableDeleteURL: 'DeleteData',
		/*bindreport*/
		showBindreport: false,
		bindreport: null,
		reportDataURL: 'LoadModel',
		reportSaveURL: 'UpdateData',
		/*map*/
		showMap: false,
		tabMapName: '地图',
		map: null,
		WMTSLayerURL: 'ReadTile',
		mapStyle: {
			type: 'drawCluster',
			zoomto: true,
			zoomlevel: 16,
			size: 16
		},
		/*custom*/
		customTab: "",
		customTabTitle: '其他 <i class="fa fa-square-o"></i>',
	};

	var methods = [
		'getOptions',
		'getSearchtree',
		'getEdittable',
		'getBindreport',
		'getMap',
		'setOptions',
		'destroy'
	];

	var vw_structure = function(element, options) {
		this.$element = $(element);
		this.options = $.extend({}, defaults, options);
		this._defaults = defaults;
		this._name = pluginName + this.$element.attr('id');
		this.version = 'v1.0.0';
		this._init();

		$(document).bind("contextmenu", function(e) {
			return false;
		});
	}

	vw_structure.prototype = {
		_init: function() {
			var that = this;
			if(!that.options.interfaceAddress) {
				return;
			}

			if(that.options.searchtree) {
				that.options.showSearchtree = true;
			}
			if(that.options.edittable) {
				that.options.showEdittable = true;
			}
			if(that.options.map) {
				that.options.showMap = true;
			}
			if(that.options.bindreport) {
				that.options.showBindreport = true;
			}

			var tabnum = 0;
			tabnum += that.options.showEdittable;
			tabnum += that.options.showBindreport;
			tabnum += that.options.showMap;
			tabnum += that.options.customTab ? 1 : 0;

			if(!tabnum) {
				return;
			}

			var HTML_SEARCHTREE = [
				'<div id="' + that._name + '-searchtree" class="' + that._name + '-left-container"></div>'
			].join("");

			var HTML_BREADCRUMB = [
				'<div class="row">',
				'	<div class="col-md-12">',
				'		<div id="' + that._name + '-breadcrumb"></div>',
				'	</div>',
				'</div>'
			].join('');

			var HTML_TAB_TABLE = [
				'<li role="presentation">',
				'	<a href="#' + that._name + '-edittable" id="tab-' + that._name + '-table" role="tab" data-toggle="tab">数据 <i class="fa fa-list-ul"></i></a>',
				'</li>'
			].join('');

			var HTML_TAB_REPORT = [
				'<li role="presentation">',
				'	<a href="#' + that._name + '-bindreport" id="tab-' + that._name + '-report" role="tab" data-toggle="tab">报表 <i class="fa fa-table"></i></a>',
				'</li>'
			].join('');

			var HTML_TAB_MAP = [
				'<li role="presentation">',
				'	<a href="#' + that._name + '-map" id="tab-' + that._name + '-map" role="tab" data-toggle="tab">' + that.options.tabMapName + ' <i class="fa fa-globe"></i></a>',
				'</li>'
			].join('');

			var HTML_TAB_CUSTOM = [
				'<li role="presentation">',
				'	<a href="#' + that._name + '-customtab" id="tab-' + that._name + '-customtab" role="tab" data-toggle="tab">' + that.options.customTabTitle + '</a>',
				'</li>'
			].join('');

			var HTML_TABS = [
				'<div class="row">',
				'	<div class="col-md-12">',
				'		<ul class="nav nav-tabs pull-right" role="tablist" style="margin-top: -40px;">',
				that.options.showEdittable ? HTML_TAB_TABLE : "",
				that.options.showBindreport ? HTML_TAB_REPORT : "",
				that.options.showMap ? HTML_TAB_MAP : "",
				that.options.customTab ? HTML_TAB_CUSTOM : "",
				'		</ul>',
				'	</div>',
				'</div>',
				'<div class="row">',
				'	<div class="col-md-12 tab-content">',
				that.options.showEdittable ? '<div id="' + that._name + '-edittable" role="tabpanel" class="tab-pane"></div>' : "",
				that.options.showBindreport ? '<div id="' + that._name + '-bindreport" role="tabpanel" class="tab-pane"></div>' : "",
				that.options.showMap ? '<div id="' + that._name + '-map" role="tabpanel" class="tab-pane"></div>' : "",
				that.options.customTab ? '<div id="' + that._name + '-customtab" role="tabpanel" class="tab-pane">' + that.options.customTab + '</div>' : "",
				'	</div>',
				'</div>'
			].join('');

			var HTML_SINGLE = [
				'<div class="row">',
				'	<div class="col-md-12">',
				that.options.showEdittable ? '<div id="' + that._name + '-edittable"></div>' : "",
				that.options.showBindreport ? '<div id="' + that._name + '-bindreport"></div>' : "",
				that.options.showMap ? '<div id="' + that._name + '-map"></div>' : "",
				that.options.customTab ? '<div id="' + that._name + '-customtab" style="margin-top:10px">' + that.options.customTab + '</div>' : "",
				'	</div>',
				'</div>'
			].join('');

			var HTML_CONTENT = [
				'	<div class="' + that._name + '-content-container">',
				that.options.showSearchtree ? HTML_BREADCRUMB : "",
				tabnum > 1 ? HTML_TABS : HTML_SINGLE,
				'	</div>'
			].join('');

			that.$element.append([
				that.options.hasContainerFluid ? '' : '<div class="container-fluid">',
				that.options.showSearchtree ? '<div class="row"><div class="col-md-1-5">' : "",
				that.options.showSearchtree ? HTML_SEARCHTREE : "",
				that.options.showSearchtree ? '</div><div class="col-md-4-5">' : "",
				HTML_CONTENT,
				that.options.showSearchtree ? '</div></div>' : "",
				that.options.hasContainerFluid ? '' : '</div>'
			].join(""));

			that.$leftContainer = $('.' + that._name + '-left-container');
			that.$contentContainer = $('.' + that._name + '-content-container');
			that.$searchtree = $('#' + that._name + '-searchtree');
			that.breadcrumbID = that._name + '-breadcrumb';
			that.$tabTable = $('#tab-' + that._name + '-table');
			that.$tabReport = $('#tab-' + that._name + '-report');
			that.$tabMap = $('#tab-' + that._name + '-map');
			that.$edittable = $('#' + that._name + '-edittable');
			that.$bindreport = $('#' + that._name + '-bindreport');
			that.$map = $('#' + that._name + '-map');

			that.$leftContainer.css({
				"background-color": "#FFFFFF",
				"padding": "10px",
				"border-radius": "4px",
				"margin-left": "-10px"
			});

			that.$contentContainer.css({
				"background-color": "#FFFFFF",
				"padding": "10px",
				"border-radius": "4px",
				"margin-left": that.options.showSearchtree ? "-20px" : "-10px",
				"margin-right": "-10px"
			});

			$(that.$contentContainer.find('li[role="presentation"]')[that.options.activeTab]).addClass('active');
			$(that.$contentContainer.find('.tab-pane')[that.options.activeTab]).addClass('active');

			that.$contentContainer.find('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
				var tabType = $(e.target).attr('id').split('-');
				that.$element.trigger("onTabShown", tabType[tabType.length - 1]);
				if(that.options.onTabShown) {
					that.options.onTabShown.call(that, tabType[tabType.length - 1]);
				}
			});

			resize();
			$(window).resize(resize);

			function resize() {
				that.$leftContainer.css("height", $(window).height() - that.options.leftMargin);
				that.$contentContainer.css("height", $(window).height() - that.options.contentMargin);
			}

			if(that.options.showSearchtree && that.$searchtree[0]) {
				if(!that.options.searchtree) {
					that.options.searchtree = {};
				}
				var _previousNode;
				that.options.searchtree.breadcrumbID = that.options.searchtree.breadcrumbID ? that.options.searchtree.breadcrumbID : that.breadcrumbID;
				that.options.searchtree.$container = that.options.searchtree.$container ? that.options.searchtree.$container : that.$leftContainer;
				that.options.searchtree.dataurl = that.options.searchtree.dataurl ? that.options.searchtree.dataurl : that.options.interfaceAddress(that.options.treeDataURL);
				that.options.searchtree.infourl = that.options.searchtree.infourl ? that.options.searchtree.infourl : that.options.interfaceAddress(that.options.treeInfoURL);
				that.options.searchtree.saveurl = that.options.searchtree.saveurl ? that.options.searchtree.saveurl : that.options.interfaceAddress(that.options.treeSaveURL);
				that.options.searchtree.deleteurl = that.options.searchtree.deleteurl ? that.options.searchtree.deleteurl : that.options.interfaceAddress(that.options.treeDeleteURL);
				that.options.searchtree.extrapara = {};
				$.extend(true, that.options.searchtree.extrapara, that.options.extrapara);
				that.$searchtree.vw_searchtree(that.options.searchtree);
				that.$searchtree.on("onTreeSelected", function(event, data) {
					if(that.options.showEdittable && that.$edittable[0]) {
						if(that.$edittable.vw_edittable('hasUncommitData')) {
							bDialog.alert('您有尚未保存的数据，切换后数据将丢失，是否仍要跳转？', function() {
								excute(data.node, "", data.type);
							}, {
								messageType: 'confirm',
								cancel: function() {
									that.$searchtree.vw_searchtree('SelectNode', _previousNode.id, true);
								}
							});
						} else {
							excute(data.node, "", data.type);
						}
					}
				});
			}

			function excute(node, type, menutype) {
				if(!node) {
					return;
				}
				that.menutype = menutype;
				if(!type || type == "data") {
					if(node.hasHtml && !that.options.bindreport) {
						that.options.edittable.type = "report";
					} else {
						that.options.edittable.type = "table";
					}

					if(node.hasJS) {
						that.options.edittable.jsFile = "js/" + node.id + ".js";
					}

					that.$edittable.vw_edittable('destroy').vw_edittable(that.options.edittable);
					if(that.options.edittable.type == "table") {
						that.$edittable.vw_edittable('ajaxLoadTableData', node.id, node.text, menutype);
					} else if(that.options.edittable.type == "report") {
						that.$edittable.vw_edittable('ajaxLoadReportData', "html/" + node.id + ".html", node.id, node.text, menutype);
					}
				}
				if((!type || type == "bind") && that.options.bindreport) {
					that.$bindreport.vw_bindreport('destroy').vw_bindreport(that.options.bindreport);
					if(node.hasHtml) {
						that.$bindreport.vw_bindreport('ajaxLoadReport', node.id, node.text);
					}
				}
				_previousNode = node;
			}

			if(that.options.showEdittable && that.$edittable[0]) {
				if(!that.options.edittable) {
					that.options.edittable = {};
				}
				that.options.edittable.$container = that.options.edittable.$container ? that.options.edittable.$container : that.$contentContainer;
				that.options.edittable.dataURL = that.options.edittable.dataURL ? that.options.edittable.dataURL : that.options.interfaceAddress(that.options.tableDataURL);
				that.options.edittable.saveURL = that.options.edittable.saveURL ? that.options.edittable.saveURL : that.options.interfaceAddress(that.options.tableSaveURL);
				that.options.edittable.submitURL = that.options.edittable.submitURL ? that.options.edittable.submitURL : that.options.interfaceAddress(that.options.tableSubmitURL);
				that.options.edittable.deleteURL = that.options.edittable.deleteURL ? that.options.edittable.deleteURL : that.options.interfaceAddress(that.options.tableDeleteURL);
				that.options.edittable.extrapara = {};
				$.extend(true, that.options.edittable.extrapara, that.options.extrapara);

				that.$edittable.vw_edittable(that.options.edittable);

				that.$tabTable.on('shown.bs.tab', function() {
					excute(_previousNode, "data", that.menutype);
				});

				//				if(that.$map[0]) {
				//					that.options.edittable.locate = {
				//						field: 'locateTo',
				//						title: '定位',
				//						events: {
				//							'click .fn_locateTo': that.options.locateTo
				//						},
				//						formatter: function(value, row, index) {
				//							return [
				//								'<a class="fn_locateTo tr_link" href="#" data-toggle="tooltip" title="定位"><i class="fa fa-globe"></i></a>'
				//							].join('   ');
				//						}
				//					};
				//				}
			}

			if(that.options.showBindreport && that.$bindreport[0]) {
				if(!that.options.bindreport) {
					that.options.bindreport = {};
				}
				that.options.bindreport.$container = that.options.bindreport.$container ? that.options.bindreport.$container : that.$contentContainer;
				that.options.bindreport.dataURL = that.options.bindreport.dataURL ? that.options.bindreport.dataURL : that.options.interfaceAddress(that.options.reportDataURL);
				that.options.bindreport.saveURL = that.options.bindreport.saveURL ? that.options.bindreport.saveURL : that.options.interfaceAddress(that.options.reportSaveURL);
				that.options.bindreport.extrapara = {};
				$.extend(true, that.options.bindreport.extrapara, that.options.extrapara);
				that.$bindreport.vw_bindreport(that.options.bindreport);

				if(that.$tabReport[0]) {
					that.$tabReport.on('shown.bs.tab', function() {
						excute(_previousNode, "bind", that.menutype);
					});
				}
			}

			if(that.options.showMap && that.$map[0]) {
				if(!that.options.map) {
					that.options.map = {};
				}
				that.options.map.$container = that.options.map.$container ? that.options.map.$container : that.$contentContainer;
				that.options.map.WMTSLayerURL = that.options.map.WMTSLayerURL ? that.options.map.WMTSLayerURL : that.options.interfaceAddress(that.options.WMTSLayerURL);
				that.options.map.extrapara = {};
				$.extend(true, that.options.map.extrapara, that.options.extrapara);

				that.options.map.defaultMapview = VW_INTERFACE.DEFAULT_MAP_VIEW;
				that.$map.vw_map(that.options.map);

				if(that.$tabMap[0]) {
					that.$tabMap.on('shown.bs.tab', function() {
						that.$map.vw_map('resetMapview');
					});
				}
			}

			that.$element.trigger("onInitialized");
			if(that.options.onInitialized) {
				that.options.onInitialized.call(that);
			}
		},

		destroy: function() {
			this.$element.empty();
		},

		getSearchtree: function() {
			return this.$searchtree;
		},

		getEdittable: function() {
			return this.$edittable;
		},

		getBindreport: function() {
			return this.$bindreport;
		},

		getMap: function() {
			return this.$map;
		},

		getOptions: function() {
			return this.options;
		}
	}

	$.fn[pluginName] = function(option) {
		var value;
		var args = Array.prototype.slice.call(arguments, 1);
		this.each(function() {
			var $this = $(this);
			var data = $this.data(pluginName);
			if(typeof option === 'string') {
				if($.inArray(option, methods) < 0) {
					throw new Error("该方法不存在: " + option);
				}
				if(!data) {
					return;
				}
				value = data[option].apply(data, args);
				if(option === 'destroy') {
					$this.removeData(pluginName);
				}
			}
			if(!data) {
				$this.data(pluginName, (data = new vw_structure(this, option)));
			}
		});
		return typeof value === 'undefined' ? this : value;
	};
});